<template>
  <div id="dialog">
    <!-- <el-button type="text" @click="dialogFormVisible = true"
      >打开嵌套表单的 Dialog</el-button
    > -->

    <el-dialog title="添加会员" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="序号" prop="id"
          ><el-input
            placeholder="请输入内容"
            v-model="ruleForm.id"
            :disabled="true"
          >
          </el-input
        ></el-form-item>
        <el-form-item label="会员姓名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="会员卡号" prop="card">
          <el-input v-model="ruleForm.card"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="会员类型" prop="type">
          <el-select v-model="ruleForm.type" placeholder="请选择会员类型">
            <el-option label="普通会员" value="普通会员"></el-option>
            <el-option label="金卡会员" value="金卡会员"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开卡时间" required>
          <el-col :span="11">
            <el-form-item prop="date1">
              <el-date-picker
                placeholder="选择日期"
                v-model="ruleForm.date1"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Bus from "../../../utils/BUS.js";
import { addVIP } from "../../../api/VIP";
import { editVIP } from "../../../api/VIP";
export default {
  data() {
    return {
      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
        type: [],
      },
      formLabelWidth: "120px",
      ruleForm: {
        id: 0, //序号
        name: "",
        card: "",
        phone: "",
        type: "",
        date1: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入会员姓名", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        card: [
          { required: true, message: "请输入会员卡号", trigger: "blur" },
          {
            min: 9,
            max: 10,
            message: "长度在 9 到 10 个字符",
            trigger: "blur",
          },
        ],
        phone: [
          { required: true, message: "请输入电话号码", trigger: "blur" },
          { min: 11, max: 11, message: "长度在11个字符", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择会员类型", trigger: "change" },
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
      },
      id1: "",
      name1: "",
      card1: "",
      phone1: "",
      date111: "",
      type1: "",
      ctrl: 0,
    };
  },
  watch: {
    dialogFormVisible: "changeid",
  },
  mounted() {
    Bus.$on("openDialog", this.toggledialog);
    Bus.$on("openEditDialog", this.toggleEditdialog);
  },
  beforeDestroy() {
    Bus.$off("openDialog", this.toggledialog);
    Bus.$off("openEditDialog", this.toggleEditdialog);
  },
  methods: {
    toggledialog() {
      this.dialogFormVisible = !this.dialogFormVisible;
      this.ctrl = 0;
    },
    toggleEditdialog(arr) {
      console.log(arr); //当前行数据
      this.ctrl = 1;
      this.id1 = arr[0];
      this.name1 = arr[1];
      this.type1 = arr[4];
      this.card1 = arr[2];
      this.phone1 = arr[3];
      this.date111 = arr[5];
      console.log(this.id1);
      this.dialogFormVisible = !this.dialogFormVisible;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //验证通过
          // 发送请求到控制台，添加数据，刷新表格
          alert("submit!");
          this.dialogFormVisible = false;
          console.log(this.ruleForm);
          if (this.ctrl == 0) {
            addVIP(this.ruleForm).then((res) => {
              if (res.code == 200) {
                console.log("succeed");
                console.log(this.$parent.getData);
                this.$parent.getData();
              }
            });
          } else {
            editVIP(this.ruleForm).then((res) => {
              if (res.code == 200) {
                console.log("succeed");
                console.log(this.$parent.getData);
                this.$parent.getData();
              }
            });
          }
        } else {
          alert("请重新输入");
          return false;
        }
      });
    },
    changeid() {
      this.$nextTick(() => {
        if (this.dialogFormVisible == true && this.ctrl == 0) {
          console.log(this.id1);
          this.ruleForm.id = this.$parent._data.total + 1;
          // console.log(this.$parent._data.viparr.length);
        } else if (this.dialogFormVisible == true && this.ctrl == 1) {
          console.log(this.id1);
          console.log(typeof this.card1);
          this.ruleForm.id = this.id1;
          this.ruleForm.name = this.name1;
          this.ruleForm.type = this.type1;
          this.ruleForm.card = this.card1 + "";
          this.ruleForm.phone = this.phone1 + "";
          this.ruleForm.date1 = this.date111;
        }
      });
    },
  },
};
</script>

<style></style>
